﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;

namespace QLKH.PopUp.DiaLy
{
    public partial class popSuaPho : Form
    {
        private Int16 iddp = -1;
        public popSuaPho(Int16 id)
        {
            InitializeComponent();
            iddp = id;
        }

        private void binCombo(ComboBox cb, DataTable dt, string name, string value, string option)
        {
            List<trangthai> list = new List<trangthai>();
            trangthai ls = null;
            ls = new trangthai();
            ls.name = option;
            ls.value = "0";
            list.Add(ls);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    ls = new trangthai();
                    ls.name = dt.Rows[i][name].ToString();
                    ls.value = dt.Rows[i][value].ToString();
                    list.Add(ls);
                }
            }
            cb.DataSource = list;
            cb.DisplayMember = "name";
            cb.ValueMember = "value";
        }

        private void popSuaPho_Load(object sender, EventArgs e)
        {
            clPX ttp = new clPX();
            DataTable dtpx = ttp.getdata(0, 0);
            binCombo(cbPX, dtpx, "ten", "id", "<Chọn Phường / Xã>");

            clPho cldp = new clPho();
            DataTable dtqh = cldp.chitiet(iddp);
            if (dtqh != null && dtqh.Rows.Count > 0)
            {
                cbPX.SelectedValue = dtqh.Rows[0]["idpx"].ToString();
                txtDuongPho.Text = dtqh.Rows[0]["ten"].ToString();
                txtVitri.Text = dtqh.Rows[0]["vitri"].ToString();
                checkHienthi.Checked = bool.Parse(dtqh.Rows[0]["hienthi"].ToString());
            }
        }

        public delegate void SuaPho(int outid);
        public SuaPho MyFunction;

        private void btsave_Click(object sender, EventArgs e)
        {
            if (MyFunction != null)
            {
                Int16 mapx = Int16.Parse(cbPX.SelectedValue.ToString());
                string tendp = txtDuongPho.Text.ToString();
                Int16 vitri = Int16.Parse((txtVitri.Text.Trim() == "") ? "0" : txtVitri.Text.Trim());
                Int16 hienthi = Int16.Parse((checkHienthi.Checked) ? "1" : "0");
                string tenkhongdau = Querys.LocDau(txtDuongPho.Text.Trim()).ToLower();

                clPho adv = new clPho();
                int outid = adv.update(iddp, mapx, tendp, vitri, hienthi, tenkhongdau);
                MyFunction(outid);
            }
            this.Close();
        }

        private void btquit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
